#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::shared_ptr;
using std::weak_ptr;

class Child;

class Parent
{
public:
	Parent()
	{ cout << "Parent()" << endl; }
	~Parent()
	{ cout << "~Parent()" << endl; }
	//只需要Child类型的指针，不需要类的完整定义
	shared_ptr<Child> _spChild;
	//weak_ptr<Child> _spChild;
};

class Child
{
public:
	Child()
	{ cout << "child()" << endl; }
	~Child()
	{ cout << "~child()" << endl; }
	//shared_ptr<Parent> _spParent;
	weak_ptr<Parent> _spParent;
};


void test(){
    //创建两个栈对象sp
    shared_ptr<Parent> spParent(new Parent());
    shared_ptr<Child> spChild(new Child());

    //访问Parent里面的数据成员_spChild
    spParent->_spChild = spChild;
    spChild->_spParent = spParent;
    //仅调用了Parent和Child的构造函数
    //但是并没有调用析构函数
    //主要的原因在于引用计数没有变为0

}

int main()
{
    test();
    return 0;
}

